package crudfx.data;
import java.io.*;
import javafx.data.pull.*;
import crudfx.util.*;
import javafx.data.xml.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class StreamProperties extends DataTree {
    public var stream:InputStream;
    public var encoding:String;
    public var tick:Boolean;
    public function load():Boolean{
        var r:Boolean=false;
        try{
            var en:String=encoding;
            if(en==null)en="UTF8";
            var current:DataTree=null;
            def parser = PullParser {
                documentType: PullParser.XML;
                input: stream;
                encoding:en
                onEvent: function(event: Event) {
                    tick=not tick;
                    if (event.type == PullParser.START_ELEMENT) {
                        if(current==null){
                            current=this;
                            this.name=event.qname.name;
                            }
                        else{
                            current=current.add(event.qname.name);
                            }
                        var attrs:QName[]=event.getAttributeNames();
                        for(a:QName in attrs){
                            var t:DataTree=DataTree{
                                name:a.toString()
                                //text:event.getAttributeValue(a)
                                parent:current
                                isAttribute:true
                                };
                            var ta:DataTree=DataTree{
                                name:""
                                text:event.getAttributeValue(a)
                                parent:t
                                };
                            //println(ta.text);
                            insert ta into t.children;
                            insert t into current.attributes;
                            }
                        }
                    else
                    if (event.type == PullParser.END_ELEMENT) {
                        current=current.parent;
                        }
                    else {
                        if (event.type == PullParser.TEXT or event.type == PullParser.CDATA) {
                            var t:DataTree=DataTree{
                                name:""
                                text:event.text
                                parent:current
                                };
                            insert t into current.children;
                            }
                        }
                    }
                };
            parser.parse();
            //parser.input.close();
            //stream.close();
            r=true;
        }catch(x){
            ConsoleIO.error(this,x);
            }
        return r;
        }
    postinit{
        try{
            load();
        }catch(x){
            ConsoleIO.error(this,x);
            x.printStackTrace();
            }
        }
    }

